
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Swaf;
using Swaf.Container;
using Swaf.Gui;

using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet
{
	public class ParamActionDropDown : Swaf.Gui.AspNet2.UserControl, IActionTrigger
    {
        protected DropDownList m_paramList;
        protected Label m_label;
        protected bool m_setupAfterEvent = false;
        protected bool m_hideOnDisabled = true;
        protected string m_dataTextField = null;
        protected string m_dataValueField = null;
        protected string m_clientScript = "";
		protected string m_bizprocActions = null;
		protected BizProcWidgetType m_procType = BizProcWidgetType.button;

		public ParamActionDropDown()
		{
		}

		public ParamActionDropDown(ActiveWidget widget)
		{
			ID = State.Id;
		}

        public bool HideOnDisabled
        {
            get { return m_hideOnDisabled; }
            set { m_hideOnDisabled = value; }
        }

		public string Actions
		{
			get { string s = (string)State.State.get("actions"); return s == null ? m_bizprocActions : s; }
			set { m_bizprocActions = value; }
		}

        public string ClientClick
        {
            get { return m_clientScript; }
            set { m_clientScript = value; }
        }

        void ActionChanged(object sender, ActionChangeType type)
        {
            setupParams();
        }

        public string DataTextField { get { return m_dataTextField; } set { m_dataTextField = value; } }
        public string DataValueField { get { return m_dataValueField; } set { m_dataValueField = value; } }

        protected void setupParams()
        {
            setupControls();
            m_paramList.Items.Clear();

			List<IAction> actions = BPC.getBizProcActions(Actions);
			if (actions != null && actions.Count != 0)
			{
				IAction action = actions[0];
				object selectedValue = State.State.get("curParam");
				if(selectedValue == null)
					selectedValue = action.CurrentParameterOption;
				foreach (object ap in action.ValidParameterOptions)
				{
					object paramTextObj = DataTextField == null ? ap : getValue(ap, DataTextField);
					object paramValueObj = DataValueField == null ? paramTextObj : getValue(ap, DataValueField);
					string paramText = paramTextObj != null ? paramTextObj.ToString() : "";
					string paramValue = paramValueObj != null ? paramValueObj.ToString() : paramText;
					ListItem i = new ListItem(paramText, paramValue);
					if (ap == selectedValue)
						i.Selected = true;
					m_paramList.Items.Add(i);
				}
				setupStateFromActions();
			}
        }

		public void setupStateFromActions()
		{
			List<IAction> actions = BPC.getBizProcActions(Actions);
			if (actions != null && actions.Count != 0)
			{
				IAction action = actions[0];
				if (!action.Enabled)
					this.Visible = !m_hideOnDisabled;
				else
					this.Visible = true;
			}
		}


        protected object getValue(object item, string fieldName)
        {
            INameAddressSupport nameItem = item as INameAddressSupport;
            if (nameItem != null)
                return nameItem.get(fieldName);
            else
                return Cmn.callPropertyGet(item, fieldName);
        }

        protected void setupControls()
        {
			List<IAction> actions = BPC.getBizProcActions(Actions);
			if (actions != null && actions.Count != 0 && m_paramList == null)
			{
				IAction action = actions[0];
				createControls();
				m_paramList = FindControl("validParams") as DropDownList;
				m_paramList.SelectedIndexChanged += new EventHandler(paramList_SelectedIndexChanged);
				m_label = FindControl("title") as Label;
				if (action != null)
					m_label.Text = action.ShortName;
			}
        }

        void paramList_SelectedIndexChanged(object sender, EventArgs e)
        {
			State.State.put("curParam", m_paramList.SelectedValue);
			List<IAction> actions = BPC.getBizProcActions(Actions);
			foreach (IAction action in actions)
				if (action.Enabled)
					action.trigger(m_paramList.SelectedValue);
			if (m_setupAfterEvent)
                setupParams();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
			setupParams();
            if (ClientClick != null)
                m_paramList.Attributes.Add("onChange", string.Format("javascript:{0}", ClientClick));
        }
		public BizProcCoordinator BPC
		{
			get { return (Page is IPage) ? ((IPage)Page).BPC : null; }
		}

		public string WidgetId { get { return ClientID; } }
		public string ParentBizProcIds { get { return Actions; } set { Actions = value; } }
		public BizProcWidgetType BizProcWidgetType { get { return m_procType; } set { m_procType = value; } }
	}
}
